#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int columns;
  int rows;
  int** data;
} Matrix;

void matrix_alloc_data(Matrix* matrix) {
    matrix -> data = (int**) calloc(matrix -> columns, sizeof(int*));
    for (int y = 0; y < matrix -> columns; y++) {
        matrix -> data[y] = (int*) calloc(matrix -> rows, sizeof(int));
    }
}

void matrix_free_data(Matrix* matrix) {
    for (int y = 0; y < matrix -> columns; y++) {
        free(matrix -> data[y]);
    }
    free(matrix -> data);
}

void matrix_initialize(Matrix* matrix) {
    int a, b;
    scanf("%d%d", &b, &a);
    if (matrix -> data != NULL) {
        matrix_free_data(matrix);
    }
    matrix -> rows = a;
    matrix -> columns = b;
    matrix_alloc_data(matrix);
    int num = 0;
    for (int y = 0; y < matrix -> columns; y++) {
        for (int x = 0; x < matrix -> rows; x++) {
            matrix -> data[y][x] = ++num;
        }
    }
}

void matrix_print(Matrix* matrix) {
    for (int y = 0; y < matrix -> columns; y++) {
        for (int x = 0; x < matrix -> rows; x++) {
            printf("%d", matrix -> data[y][x]);
            if (x != matrix -> rows - 1) {
                putchar(' ');
            }
        }
        putchar('\n');
    }
    putchar('\n');
}

void matrix_switch_row(Matrix* matrix) {
    int a, b;
    scanf("%d%d\n", &a, &b);
    a--;
    b--;
    for (int y = 0; y < matrix -> columns; y++) {
        int tmp = matrix -> data[y][a];
        matrix -> data[y][a] = matrix -> data[y][b];
        matrix -> data[y][b] = tmp;
    }
}

void matrix_switch_column(Matrix* matrix) {
    int a, b;
    scanf("%d%d\n", &a, &b);
    a--;
    b--;
    int* tmp = matrix -> data[a];
    matrix -> data[a] = matrix -> data[b];
    matrix -> data[b] = tmp;
}

void matrix_transform(Matrix* matrix) {
    Matrix new_matrix;
    new_matrix.data = NULL;
    new_matrix.rows = matrix -> columns;
    new_matrix.columns = matrix -> rows;
    matrix_alloc_data(&new_matrix);
    for (int y = 0; y < matrix -> columns; y++) {
        for (int x = 0; x < matrix -> rows; x++) {
            new_matrix.data[x][y] = matrix -> data[y][x];
        }
    }
    matrix_free_data(matrix);
    matrix -> data = new_matrix.data;
    matrix -> rows = new_matrix.rows;
    matrix -> columns = new_matrix.columns;
}

void matrix_filp_column(Matrix* matrix) {
    Matrix new_matrix;
    new_matrix.data = NULL;
    new_matrix.rows = matrix -> rows;
    new_matrix.columns = matrix -> columns;
    new_matrix.data = (int**) calloc(matrix -> columns, sizeof(int*));
    for (int y = 0; y < matrix -> columns; y++) {
        new_matrix.data[y] = matrix -> data[(matrix -> columns) - y - 1];
    }
    free(matrix -> data);
    matrix -> data = new_matrix.data;
    matrix -> rows = new_matrix.rows;
    matrix -> columns = new_matrix.columns;
}

void matrix_filp_row(Matrix* matrix) {
    Matrix new_matrix;
    new_matrix.data = NULL;
    new_matrix.rows = matrix -> rows;
    new_matrix.columns = matrix -> columns;
    matrix_alloc_data(&new_matrix);
    for (int y = 0; y < matrix -> columns; y++) {
        for (int x = 0; x < matrix -> rows; x++) {
            new_matrix.data[y][(matrix -> rows) - x - 1] = matrix -> data[y][x];
        }
    }
    matrix_free_data(matrix);
    matrix -> data = new_matrix.data;
}

typedef void(*matrix_function) (Matrix* matrix);

typedef struct {
    const char* name;
    const matrix_function func;
} matrix_operation;

matrix_operation matrix_functions[] = {
    {"IN", matrix_initialize},
    {"SR", matrix_switch_column},
    {"SC", matrix_switch_row},
    {"TR", matrix_transform},
    {"FR", matrix_filp_column},
    {"FC", matrix_filp_row},
    {"PR", matrix_print}
};

int main(void) {
    Matrix matrix;
    matrix.data = NULL;
    while (1) {
        char command[16];
        if (scanf(" %16s", command) == EOF) {
            break;
        }
        for (int i = 0;
            i < sizeof(matrix_functions) / sizeof(matrix_operation); i++) {
            if (strcmp(command, matrix_functions[i].name) == 0) {
                matrix_functions[i].func(&matrix);
                goto command_complete;
            }
        }
        puts("Invaild command!");
        command_complete:
        continue;
    }
    matrix_free_data(&matrix);
    return 0;
}

